package com.zeng.jiudian.dao.impl;

import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.bean.ProductTypeModel;
import com.zeng.jiudian.bean.ProductTypeModelExample;
import com.zeng.jiudian.bean.ProductTypeModelExample.Criteria;
import com.zeng.jiudian.dao.ProductTypeDao;
import com.zeng.jiudian.mapper.ProductTypeMapper;
import com.zeng.jiudian.utils.MyBatisFactory;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/8/12 - 11:01
 */
public class ProductTypeDaoImpl implements ProductTypeDao
{
    private SqlSession sqlSession;

    @Override
    public int add(ProductTypeModel productTypeModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        productTypeModel.setCreateTime(dateFormat.format(new Date()));
        productTypeModel.setUpdateTime(dateFormat.format(new Date()));

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            count = mapper.insert(productTypeModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<ProductTypeModel> findAll(ProductTypeModel productTypeModel)
    {
        List<ProductTypeModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            ProductTypeModelExample example = new ProductTypeModelExample();
            Criteria criteria = example.createCriteria();
            if (!"".equals(productTypeModel.getProductType()) && productTypeModel.getProductType() != null)
            {
                criteria.andProductTypeLike("%" + productTypeModel.getProductType() + "%");
            }
            if (!"".equals(productTypeModel.getEnType()) && productTypeModel.getEnType() != null)
            {
                criteria.andEnTypeLike("%" + productTypeModel.getEnType() + "%");
            }
            if (productTypeModel.getIsShow() != null && productTypeModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(productTypeModel.getIsShow());
            }
            if (productTypeModel.getRecommend() != null && productTypeModel.getRecommend() >= 0)
            {
                criteria.andRecommendEqualTo(productTypeModel.getRecommend());
            }
            if (!"无".equals(productTypeModel.getOrder()) && productTypeModel.getOrder() != null)
            {
                if ("创建时间".equals(productTypeModel.getOrder()))
                {
                    if ("降序".equals(productTypeModel.getLift()))
                    {
                        example.setOrderByClause("create_time desc");
                    }
                    else if ("升序".equals(productTypeModel.getLift()))
                    {
                        example.setOrderByClause("create_time asc");
                    }
                }
                else if ("更新时间".equals(productTypeModel.getOrder()))
                {
                    if ("降序".equals(productTypeModel.getLift()))
                    {
                        example.setOrderByClause("update_time desc");
                    }
                    else if ("升序".equals(productTypeModel.getLift()))
                    {
                        example.setOrderByClause("update_time asc");
                    }
                }
            }
            else
            {
                if ("降序".equals(productTypeModel.getLift()))
                {
                    example.setOrderByClause("id desc");
                }
                else if ("升序".equals(productTypeModel.getLift()))
                {
                    example.setOrderByClause("id asc");
                }
            }
            PageModel pageModel = productTypeModel.getPageModel();
            if (pageModel != null)
            {
                Integer nowNum = pageModel.getNowNum();
                Integer pageSize = pageModel.getPageSize();
                if (nowNum != null && pageSize != 0 && pageSize != null)
                {
                    example.setStartRow((nowNum - 1) * pageSize);
                    example.setPageSize(pageSize);
                }
            }
            list = mapper.selectByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }

    @Override
    public int getCount(ProductTypeModel productTypeModel)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            ProductTypeModelExample example = new ProductTypeModelExample();
            Criteria criteria = example.createCriteria();
            if (!"".equals(productTypeModel.getProductType()) && productTypeModel.getProductType() != null)
            {
                criteria.andProductTypeLike("%" + productTypeModel.getProductType() + "%");
            }
            if (!"".equals(productTypeModel.getEnType()) && productTypeModel.getEnType() != null)
            {
                criteria.andEnTypeLike("%" + productTypeModel.getEnType() + "%");
            }
            if (productTypeModel.getIsShow() != null && productTypeModel.getIsShow() >= 0)
            {
                criteria.andIsShowEqualTo(productTypeModel.getIsShow());
            }
            if (productTypeModel.getRecommend() != null && productTypeModel.getRecommend() >= 0)
            {
                criteria.andRecommendEqualTo(productTypeModel.getRecommend());
            }

            count = (int) mapper.countByExample(example);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public ProductTypeModel findById(int id)
    {
        ProductTypeModel model = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            model = mapper.selectByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return model;
    }

    @Override
    public int delete(int id)
    {
        int count = 0;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            count = mapper.deleteByPrimaryKey(id);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public int update(ProductTypeModel productTypeModel)
    {
        int count = 0;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        productTypeModel.setUpdateTime(dateFormat.format(new Date()));

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            count = mapper.updateByPrimaryKeySelective(productTypeModel);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return count;
    }

    @Override
    public List<ProductTypeModel> getAll()
    {
        List<ProductTypeModel> list = null;

        try
        {
            sqlSession = MyBatisFactory.getSqlSession();
            ProductTypeMapper mapper = sqlSession.getMapper(ProductTypeMapper.class);
            list = mapper.selectByExample(null);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            sqlSession.close();
        }

        return list;
    }
}
